<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>注册</title>
  <!-- 导入jquery -->
  <script src="/node_modules/jquery/dist/jquery.min.js"></script>
  <!-- 导入bootstrap -->
  <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css">
  <script src="/node_modules/bootstrap/dist/js/bootstrap.js"></script>
  <!-- 导入art-template -->
  <script src="/node_modules/art-template/lib/template-web.js"></script>
  <!-- 客户端md5加密包 -->
  <script src="/resource/md5.min.js"></script>
  <style>
    /* 把登录框往下压 */
    .my-panel {
      margin-top: 200px;
    }
  </style>
</head>

<body>
  <!-- bootstrap的容器  带版心的-->
  <div class="container">
    <!-- 栅格的行 -->
    <div class="row">
      <!-- 栅格的列 offset 列偏移 -->
      <div class="col-xs-6 col-xs-offset-3">
        <!-- 面板 -->
        <div class="panel panel-default panel-primary my-panel">
          <!-- 面板的标题 -->
          <div class="panel-heading">
            <h3 class="panel-title">登录CQ管理系统</h3>
          </div>
          <!-- 面板的内容 -->
          <div class="panel-body">
            <!-- 表单 -->
            <form class="form-horizontal" id="form">
              <div class="form-group">
                <label for="userName" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                  <input type="text" name="userName" class="form-control" id="userName" placeholder="王**">
                </div>
              </div>
              <div class="form-group">
                <label for="passWord" class="col-sm-2 control-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                <div class="col-sm-10">
                  <input type="password" name="passWord" class="form-control" id="passWord" placeholder="密码">
                </div>
              </div>
              <div class="form-group">
                <label for="code" class="col-sm-2 control-label">验证码</label>
                <div class="col-sm-6">
                  <input type="text" name="code" class="form-control" id="code" placeholder="验证码">
                </div>
                <div class="col-sm-4">
                  <img class="code" src="/captcha" alt="">
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-danger" id="register" value="注册">
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script>
    $(function () {
      $('#form').on('submit', function (e) {
        e = e || window.event;
        e.preventDefault();
        /* 
          对密码进行md5加盐加密: 让加密的过程更加的安全
        */
        // 加密之后直接将密文显示在表单中
        $('#passWord').val(md5($('#passWord').val(), 'add solt'))

        $.ajax({
          url: '/register',
          type: 'post',
          dataType: 'json',
          data: $(this).serialize(),
          success: function (obj) {
            // 0: 注册成功 1: 用户存在 2: 验证码错误 500:服务器错误
            if(obj.err_code ==0 ){
              location.href = '/resource/view/login.html'
            }else{
              alert(obj.err_msg);
            }
          }
        })
      })
    })
  </script>
</body>

</html>